package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.UserCollection;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.IUserCollectionService;
import com.mars.module.admin.request.UserCollectionRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户收藏控制层
 *
 * @author mars
 * @date 2024-03-04
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "4.用户收藏接口管理", tags = "4.用户收藏接口管理")
@RequestMapping("/admin/userCollection")
public class UserCollectionController {

    private final IUserCollectionService iUserCollectionService;

    /**
     * 分页查询用户收藏列表
     */
    @ApiOperation(value = "分页查询用户收藏列表")
    @PostMapping("/pageList")
    public R<PageInfo<UserCollection>> pageList(@RequestBody UserCollectionRequest userCollectionRequest) {
        return R.success(iUserCollectionService.pageList(userCollectionRequest));
    }

    /**
     * 获取用户收藏详细信息
     */
    @ApiOperation(value = "获取用户收藏详细信息")
    @GetMapping(value = "/query/{id}")
    public R<UserCollection> detail(@PathVariable("id") Long id) {
        return R.success(iUserCollectionService.getById(id));
    }

    /**
     * 新增用户收藏
     */
    @Log(title = "新增用户收藏", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增用户收藏")
    @PostMapping("/add")
    public R<Void> add(@RequestBody UserCollectionRequest userCollectionRequest) {
        iUserCollectionService.add(userCollectionRequest);
        return R.success();
    }

    /**
     * 修改用户收藏
     */
    @Log(title = "修改用户收藏", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户收藏")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody UserCollectionRequest userCollectionRequest) {
        iUserCollectionService.update(userCollectionRequest);
        return R.success();
    }

    /**
     * 删除用户收藏
     */
    @Log(title = "删除用户收藏", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户收藏")
    @PostMapping("/delete")
    public R<Void> remove(@RequestParam(value = "newsId") String newsId, @RequestParam(value = "userId") String userId) {
        iUserCollectionService.remove(newsId, userId);
        return R.success();
    }

    /**
     * 导出用户收藏
     *
     * @param response response
     * @throws IOException IOException
     */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response, @RequestBody UserCollectionRequest userCollectionRequest) throws IOException {
        List<UserCollection> list = iUserCollectionService.list(userCollectionRequest);
        ExcelUtils.exportExcel(list, UserCollection.class, "用户收藏信息", response);
    }
}
